package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-11-09
 * Time：08:43
 */
public class ReflectClassDemo {
    //反射创建对象
    public static void  reflectNewInstance()  {
        try {
            Class<?> c3 = Class.forName("reflectdemo.Student");
            Student student = (Student)c3.newInstance();
            System.out.println(student);
            student.sleep();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    //通过反射 调用构造方法
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c3 = Class.forName("reflectdemo.Student");
            Constructor<?> constructor = c3.getDeclaredConstructor(String.class, int.class);

            constructor.setAccessible(true);

            Student student = (Student) constructor.newInstance("Ken", 20);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //反射 调用私有属性
    public static void reflectPrivateField() {
        try {
            Class<?> c3 = Class.forName("reflectdemo.Student");
            Student student = (Student)c3.newInstance();

            Field field = c3.getDeclaredField("name");
            field.setAccessible(true);

            field.set(student, "cs");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectPrivateMethod() {
        try {
            Class<?> c3 = Class.forName("reflectdemo.Student");
            Student student = (Student)c3.newInstance();

            Method method = c3.getDeclaredMethod("function", String.class);
            method.setAccessible(true);
            method.invoke(student, "send a parameter...");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }
}
